<template>
  <div class="tree-container">
    <div class="left-tree">
      <div class="filter-container">
        <div class="tree-header">
          <span class="page-title">物料分类</span>
          <div class="btn-list">
            <p class="btn-item" @click="handleCreate({ level: 0 })">
              <i class="el-icon-plus" />
            </p>
            <p class="btn-item" @click="handleReset">
              <i class="el-icon-refresh-left" />
            </p>
          </div>
        </div>
        <div class="tree-list">
          <el-tree
            :props="treeProps"
            :data="treeData"
            node-key="id"
            default-expand-all
            :expand-on-click-node="false"
            children="child"
            @node-click="handleNodeClick"
          >
            <span class="custom-tree-node" slot-scope="{ node, data }">
              <span class="label">{{ data.name }}</span>
              <span>
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-circle-plus-outline"
                  v-if="data.level === 1"
                  @click="() => handleCreate(data)"
                >
                </el-button>
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-edit"
                  @click="() => handleEditRow(data)"
                >
                </el-button>
                <el-button
                  type="text"
                  size="mini"
                  icon="el-icon-delete"
                  style="color: red"
                  @click="() => handleDelete(data)"
                >
                </el-button>
              </span>
            </span>
          </el-tree>
        </div>
      </div>
    </div>
    <div class="right-container">
      <page-header-tool
        :title="title"
        :btns="modBtns"
        :max="4"
      ></page-header-tool>
      <avue-crud
        class="avue-affixed avue-new-table"
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :option="tableOption"
        :table-loading="listLoading"
        @refresh-change="searchChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @search-change="searchChange"
        @selection-change="selectionChange"
      >
        <template slot="status" slot-scope="{ index, row }">
          <el-tag size="mini" type="success" v-if="row.status === '1'"
            >启用</el-tag
          >
          <el-tag size="mini" type="warning" v-if="row.status === '0'"
            >停用</el-tag
          >
        </template>
        <template slot-scope="scope" slot="menu">
          <table-column-btns
            :btns="tableBtns"
            :scope="scope"
          ></table-column-btns>
        </template>
      </avue-crud>
    </div>
  </div>
</template>

<script>
import { tableOption } from "./const/options";
import { tableBaseMixin } from "@/mixins/tableBase";
import { materialClassData } from "./const/data.js";
export default {
  name: "ledger",
  mixins: [tableBaseMixin],
  data() {
    return {
      treeProps: {
        children: "childList",
        label: "name",
      },
      modBtns: [
        {
          name: "新增",
          click: this.handleCreatedTable,
          type: "success",
        },
      ],
      treeData: [],
      tableOption,

      selectionData: [],
      tableData: [],
      listLoading: false,
      tableBtns: [
        {
          name: "编辑",
          click: this.handleEditRow,
        },
        {
          name: "删除",
          click: this.handleDelete,
          color: "red",
        },
        {
          name: "启用",
          click: this.handleEnable,
          show: (row, index) => {
            return row.status === "0";
          },
        },
        {
          name: "停用",
          click: this.handleStop,
          color: "#E6A23C",
          show: (row, index) => {
            return row.status === "1";
          },
        },
      ],
      title: "请选择",
    };
  },
  computed: {},
  created() {
    this.init();
  },
  methods: {
    init() {
      this.page.currentPage = 1;
      this.getTree();
    },
     getTree() {
        console.log(materialClassData,"materialClassData is")
        this.treeData = materialClassData;
    },
    // 节点点击
    handleNodeClick(data) {
      if (data.level === 1) {
        let firstLevel = "";
        this.treeData.forEach((item) => {
          if (item.id === data.id) {
            firstLevel = item.name;
            item.childList.forEach((cItem) => {
              cItem.fatherName = item.name;
            });
            this.tableData = item.childList;
          }
        });
        this.title = `${firstLevel}`;
        this.currentObj = data;
      }
    },

    handleReset() {
      this.getTree();
      this.currentObj = { code: null, parentCode: null };
      this.title = "请选择单据类型";
      this.tableData = [];
    },
    /* C 新增主数据 */
    handleCreate(row) {
      this.$refs.modifyDialog.add(row);
    },
    /* U 编辑主数据 */
    handleEditRow(row, index) {
      this.$refs.modifyDialog.edit(row);
    },
    handleCreatedTable() {
      if (this.currentObj && this.currentObj.level === 1) {
        this.$refs.modifyDialog.add(this.currentObj);
      }
    },
    /* 删除数据 */
    handleDelete(row) {
      this.$confirm("是否确认删除该信息", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {});
    },
    // 删除仓库
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.tree-container {
  display: flex;
  padding: 0 10px;
  width: 100%;
  height: 100%;
  .left-tree {
    margin-right: 15px;
    width: 20%;
    .filter-container {
      height: 100%;
      background-color: #ffffff;
      padding: 10px;
      .tree-header {
        height: 24px;
        line-height: 24px;
        font-size: 16px;
        font-weight: bold;
        display: flex;
        justify-content: space-between;
        width: 100%;
        margin-bottom: 20px;
        .btn-list {
          display: flex;
          justify-content: space-between;
          .btn-item {
            margin: 0 8px 0 0;
            cursor: pointer;
          }
        }
      }
    }
    .custom-tree-node {
      display: flex;
      justify-content: space-between;
      width: 100%;
      font-size: 12px;
      padding: 0 10px;
      .label {
        line-height: 28px;
      }
    }
  }
  .right-container {
    width: 80%;
    height: 100%;
    background-color: #ffffff;
  }
}
</style>
